<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Zoom Image Point</title>
    <style>
      * {
        padding: 0;
        margin: 0;
        outline: 0;
        overflow: hidden;
      }
      html,
      body {
        margin: 0;
        padding: 0;
        width: 100%;
        height: 100%;
        overflow: hidden;
      }
      .zoom_outer {
        width: 100vw;
        height: 100vh;
      }
      #zoom {
        width: 100px;
        height: 100px;
        transform-origin: 0px 0px;
        transform: scale(1) translate(0px, 0px);
        cursor: grab;
        background: rgba(255, 166, 0, 0.432);
      }
      div#zoom > img {
        width: 100%;
        height: auto;
      }
    </style>
  </head>
  <body>
    <div class="zoom_outer">
      <div id="zoom"></div>
    </div>
    <script>
      var zoom = document.getElementById('zoom'),
        scale = 1,
        start = { x: 0, y: 0 },
        point = { x: 0, y: 0 },
        flag = false;

      function transfrom() {
        console.log(point);
        zoom.style.transform = `translate(${point.x}px,${point.y}px) scale(${scale})`;
      }

      // 鼠标摁下的时候确认当前鼠标指针的相对位置，参照物为 zoom
      zoom.onmousedown = e => {
        e.preventDefault();
        flag = true;
        // 确定原点
        start = { x: e.clientX - point.x, y: e.clientY - point.y };
      };

      // 鼠标抬起时关闭 flag
      zoom.onmouseup = () => {
        flag = false;
      };

      // 鼠标指针移动动态更改鼠标指针的参考坐标
      zoom.onmousemove = e => {
        if (!flag) return;
        e.preventDefault();

        point = {
          x: e.clientX - start.x,
          y: e.clientY - start.y
        };

        transfrom();
      };

      // 滚轮滚动调整缩放
      zoom.onwheel = e => {
        e.preventDefault();
        var xs = (e.clientX - point.x) / scale,
          ys = (e.clientY - point.y) / scale,
          delta = e.wheelDelta ? e.wheelDelta : -e.deltaY;

        delta > 0 ? (scale *= 1.2) : (scale /= 1.2);

        point = {
          x: e.clientX - xs * scale,
          y: e.clientY - ys * scale
        };

        transfrom();
      };
    </script>
  </body>
</html>
